package com.example.yls.demoa;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private MyReceiver receiver;
    private Button btnStart;
    private TextView txtTime;

    @SuppressLint("CutPasteId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e("MyService", "thread id ="+Thread.currentThread().getId());
        receiver = new MyReceiver(MainActivity.this);
        IntentFilter filter=new IntentFilter();
        filter.addAction("TIME_CHANGE");
        registerReceiver(receiver,filter);

        btnStart=findViewById(R.id.btn_start);
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                startService(intent);
            }
        });
        txtTime=findViewById(R.id.txt_time);

    }

    public void show(String timeStr) {
        txtTime.setText(timeStr);
    }


}
